import { IEngineTsCodeMapKeys } from './engineTs'

const engineJsCodeMap: {
    [key in IEngineTsCodeMapKeys]: string
} = {
    actInfo: `/**
 * 获取活动信息
 */
export const getEngineActInfoApi = async () => {
	const { engine } = await useAppContext()
	const res = await engine.getActInfo()
	return res
}`,

    collection: `/**
 * 获取用户图鉴列表
 */
export const getEngineCollectionApi = async () => {
	const { engine, appConfig } = await useAppContext()
	const res = await engine.getCollection({
		collectionKey: appConfig.CollectionKey,
	})
	return res
}`,

    configs: `/**
 * 获取引擎配置
 */
export const getEngineConfigsApi = async () => {
	const { engine } = await useAppContext()
	const res = await engine.getActConfig()
	if (res.code !== 0) {
		return
	}
	const configs = res.data?.configs?.reduce(
		(acc, curr) => {
			const key = curr.key
			let value = curr.value
			try {
				value = JSON.parse(value)
			} catch {
				console.log('cannot parse', key, value)
			}
			acc[key] = value
			return acc
		},
		{}
	)
	return configs
}`,

    drawPreview: `/**
 * 获取引擎抽奖预览
 */
export const getEngineDrawPreviewApi = async (drawKey) => {
	const { engine } = await useAppContext()
	const res = await engine.getDrawPreview({
		drawKey,
	})
	return res
}`,

    exchanges: `/**
 * 获取引擎兑换列表
 */
export const getEngineExchangesApi = async () => {
	const { engine, appConfig } = await useAppContext()
	const { ExchangeKey } = appConfig
	const res = await engine.getExchangeList({
		exchangeKey: ExchangeKey,
	})
	return res
}`,

    levels: `/**
 * 获取等级列表
 */
export const getEngineLevelsApi = async () => {
	const { engine, appConfig } = await useAppContext()
	const LevelKey = appConfig
	const res = await engine.getLevelList({
		levelKey: LevelKey.LevelKey || '',
	})
	return res
}`,

    records: `/**
 * 获取抽奖记录
 */
export const getEngineRecordsApi = async () => {
	const { engine } = await useAppContext()
	const res = await engine.getRecordList({
		page: {
			page: 1,
			limit: 100,
		},
	})
	return res
}`,

    rank: `/**
 * 获取总榜
 */
export const getEngineRankApi = async (rankKey) => {
	const { engine } = await useAppContext()
	const res = await engine.getRankStruct({
		rankId: rankKey,
	})
	return res
}`,

    tasks: `/**
 * 获取引擎任务列表
 */
export const getEngineTasksApi = async () => {
	const { engine } = await useAppContext()
	const res = await engine.getTaskList({})
	return res
}`,

    rewardDetail: `/**
 * 获取引擎奖励详情
 */
export const getEngineRewardDetailApi = async (id, type) => {
	const { engine, appConfig } = await useAppContext()
	const res = await engine.getPrizeDetail({
		id: id,
		type: type,
		actId: appConfig?.ActivityKey || '',
	})
	return res
}`,
}

export const engineJs = (keys: IEngineTsCodeMapKeys[]) => {
    const values = keys.map((key) => engineJsCodeMap[key])

    return `import { useAppContext } from './useAppContext'

${values.join('\n\n')}
`
}
